package MockDb;

import java.util.ArrayList;

import java.util.List;
import java.util.Random;

import DbObjects.Address;
import DbObjects.Person;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.lang.instrument.Instrumentation;

public class StaticMockDbInitiailizer implements IMockDbInitializer<Person>
{
	long size = 0;
	private final String[] FIRST_NAMES = 
		{
			"Alice",
			"John",
			"Sarah",
			"Serena",
			"Steve",
			"Jack",
			"Meryl",
			"Ivy",
			"Scott",
			"Trish"
		};
	
	private final String[] LAST_NAMES =
		{
			"Jones",
			"Smith",
			"Doe",
			"Burnett",
			"Marshall",
			"Matthews",
			"Cutler",
			"Rodgers",
			"Williams",
			"Drew"
		};
	
	private final String[] STREET_NAMES =
		{
			"Teak Ave",
			"Duval Rd",
			"8th St",
			"Centurion Rd",
			"Southside Blvd",
			"Baymeadows Rd",
			"UNF Dr",
			"Phillips Hwy",
			"Spinnaker Lane",
			"Magnolia Ave"
		};
	
	private final String[] STATES =
		{
			"AL", "FL", "AZ", "MD", "AZ",
			"NC", "SC", "TN", "VT", "OK",
			"TX", "MO", "MS", "WA", "ID"
		};
	
	private final String[] CITIES =
		{
			"Waterville",
			"Jacksonville",
			"Boston",
			"Dallas",
			"Bismarck",
			"Shalimar",
			"Nashville",
			"Atlanta",
			"Tampa",
			"Houston"
		};
	
	private Random rand;
	
	public StaticMockDbInitiailizer(long size)
	{
		this.size = size;
		rand = new Random();
	}
	public StaticMockDbInitiailizer()
	{
		rand = new Random();
	}
	

	
	@Override
	public Iterable<Person> InitializeDb() 
	{
		if(size == 0)
		{
			int minRecs = 5;
			int maxRecs = 20;
			
			int personCount = 0;
			int numPersons = rand.nextInt(maxRecs - minRecs + 1) + minRecs;
			
			List<Person> personList = new ArrayList<Person>();
			
			for(int x=0; x<numPersons; x++)
			{
				Person p = GeneratePerson();
				p.setPersonId(++personCount);
				p.setAddress(GenerateAddress());
				personList.add(p);
			}
			
			return (Iterable<Person>)personList;
		}
		else
		{
			List<Person> personList = new ArrayList<Person>();
			int personCount = 0;
			try
			{
				ByteArrayOutputStream bOut = new ByteArrayOutputStream();  
				ObjectOutputStream oOut = new ObjectOutputStream(bOut);   
				
				while(bOut.toByteArray().length < size)
				{
					Person p = GeneratePerson();
					p.setPersonId(++personCount);
					p.setAddress(GenerateAddress());
					personList.add(p);
					oOut.writeObject(p);  
				}
				
				oOut.close();  
			}
			catch(Exception e)
			{
				
			}
			return (Iterable<Person>)personList;
		}
	}
	
	private Person GeneratePerson()
	{
		Person p = new Person();
		
		p.setFirstName(this.FIRST_NAMES[rand.nextInt(FIRST_NAMES.length)]);
		p.setLastName(this.LAST_NAMES[rand.nextInt(LAST_NAMES.length)]);
		
		return p;
	}
	
	private Address GenerateAddress()
	{
		Address addr = new Address();
		
		addr.setAddressId(1);
		addr.setStreetNumber(Integer.toString(rand.nextInt(9999 - 99 + 1) + 1));
		addr.setStreetAddress(this.STREET_NAMES[rand.nextInt(STREET_NAMES.length)]);
		addr.setCity(this.CITIES[rand.nextInt(CITIES.length)]);
		addr.setState(this.STATES[rand.nextInt(STATES.length)]);
		addr.setZip(Integer.toString(rand.nextInt(99999 - 10001 + 1) + 1));
		
		return addr;
	}
}
